import { App } from "../../../../src/App";
import MailSys from "../../../src/system/MailSys";
import PropSys from "../../../src/system/PropSys";

const {ccclass, property} = cc._decorator;

@ccclass
export default class Mail extends cc.Component {

    @property(cc.Label)
    lblTitle: cc.Label = null;
    @property(cc.ScrollView)
    mailList: cc.ScrollView = null;
    @property(cc.Node)
    mailItem: cc.Node = null;
    @property(cc.Node)
    btnDelete: cc.Node = null;
    
    @property(cc.Node)
    contentNode: cc.Node = null;
    @property(cc.Node)
    goodsList: cc.Node = null;
    @property(cc.Node)
    goodsItem: cc.Node = null;
    @property(cc.RichText)
    lblContent: cc.RichText = null;
    @property(cc.Label)
    lblSender: cc.Label = null;
    @property(cc.Label)
    lblDate: cc.Label = null;

    private _titles:string[] = [];
    private _openMail:any = null;
    private _deleteList:any[] = [];
    onLoad(){
        this._titles = App.getText('mail.title').split('|')
        this.lblTitle.string = this._titles[0];
        this.contentNode.active = false;
        this.mailList.node.active = true;
        this.btnDelete.active = false;
    }
    start(){
        const content:cc.Node = App.Util.find('view/content',this.mailList);
        content.removeAllChildren();
        MailSys.mailList.forEach((mail)=>{
            const item:cc.Node = cc.instantiate(this.mailItem);
            content.addChild(item);
            // @ts-ignore
            item.mail = mail;
            item.getChildByName('imgOpen').active = !mail.open;
            item.getChildByName('imgGoods').active = mail.annex.length > 0;
            App.setLableText(item,'lblSender',App.Util.format(App.getText('mail.item_sender'),mail.sender));
            App.setLableText(item,'lblDate',App.Util.format(App.getText('mail.sender_time'),App.Util.formatTimeStamp(mail.timeStamp)));

            const clickEvent = new cc.Component.EventHandler();
            clickEvent.target = this.node; // 这个 node 节点是你的事件处理代码组件所属的节点
            clickEvent.component = 'Mail';// 这个是代码文件名
            clickEvent.handler = 'onItemClick';
            clickEvent.customEventData = JSON.stringify(mail);
            item.getChildByName('btnOpen').getComponent('TouchNode').clickEvent = clickEvent;

            
            const toggle = item.getChildByName('btnSelect').getComponent(cc.Toggle);
            toggle.isChecked = false;
            const checkedEvent = new cc.Component.EventHandler();
            checkedEvent.target = this.node; // 这个 node 节点是你的事件处理代码组件所属的节点
            checkedEvent.component = 'Mail';// 这个是代码文件名
            checkedEvent.handler = 'onCheckedClicket';
            checkedEvent.customEventData = JSON.stringify(mail);
            toggle.checkEvents.push(checkedEvent);
        });

    }

    onItemClick(_,data){
        cc.log(data);
        const mail = JSON.parse(data);
        if (!mail) return ;
        MailSys.openMail(mail.id);
        this._openMail = mail;
        this.mailList.node.active = false;
        this.contentNode.active = true;
        
        this.lblTitle.string = this._titles[1];
        this.lblSender.string = App.Util.format(App.getText('mail.item_sender'),mail.sender);
        this.lblDate.string = App.Util.format(App.getText('mail.sender_time'),App.Util.formatTimeStamp(mail.timeStamp));
        this.lblContent.string = `<color=#4b4a49>${mail.text}</c>`;
        this.goodsList.removeAllChildren();
        this.contentNode.getChildByName('btnGetAnnex').active = mail.annex.length > 0;
        if (mail.annex){
            for (let goods of mail.annex){
                const item = cc.instantiate(this.goodsItem);
                this.goodsList.addChild(item);
                PropSys.setIcon(goods.id,item.getChildByName('imgIcon').getComponent(cc.Sprite));
                App.setLableText(item,'lblCount',`X${goods.count}`);
            }
        }
    }

    onBackClicket(){
        if (!this._openMail) return ;

        const all = App.Util.find<cc.Node>('view/content',this.mailList).children;
        for (let it of all){
            // @ts-ignore
            if (it.mail && it.mail.id === this._openMail.id){
                it.getChildByName('imgOpen').active = false;
                it.getChildByName('imgGoods').active = this._openMail.annex.length > 0;
                this.lblTitle.string = this._titles[0];
                this.mailList.node.active = true;
                this.contentNode.active = false;
                this._openMail = null;
                return ;
            }
        }
    }

    onCollectClicket(){
        if (!this._openMail || this._openMail.annex.length < 1) return ;
        if (MailSys.removeAnnex(this._openMail.id)){
            this.goodsList.removeAllChildren();
            this._openMail.annex = [];
            this.contentNode.getChildByName('btnGetAnnex').active = false;
        }
    }

    onDeleteClicket(){
        if (this._deleteList.length < 1) return ;
        App.MessageBox(App.Util.format(App.getText('mail.remove_mails'),this._deleteList.length),(flag:number)=>{
            if (flag === App.MB_FLAG.OK){
                MailSys.remove(this._deleteList.slice(0),(is:boolean, all:any[])=>{
                    if (!is) return ;
                    const content:cc.Node = App.Util.find('view/content',this.mailList);
                    while (this._deleteList.length > 0){
                        const id = this._deleteList.shift();
                        let idx = 0;
                        for (let it of content.children){
                            // @ts-ignore
                            if (it.mail && it.mail.id === id){
                                content.removeChild(it);    
                                break ;
                            }
                            idx++;
                        }
                    }
                    App.popTips(App.getText('mail.remove_success'));
                    this._deleteList = [];
                    this.btnDelete.active = false;
                });
            }
        });
    }

    onCheckedClicket(toggle:cc.Toggle, customEventData:string){
        const mail = JSON.parse(customEventData);
        if (mail.annex.length){
            App.popTips(App.getText('mail.has_goods'));
            toggle.isChecked = false;
            return ;
        }
        
        const idx = this._deleteList.indexOf(mail.id);
        if (!toggle.isChecked){
            idx > -1 && this._deleteList.splice(idx,1);
        } else {
            idx < 0 && this._deleteList.push(mail.id);
        }
        if (this._deleteList.length > 0){
            this.btnDelete.getComponent(cc.Label).string = App.Util.format(App.getText('mail.select_remove'),this._deleteList.length);
        }
        this.btnDelete.active = this._deleteList.length > 0;
    }
}
